﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LenderClearingHouse
{
  public partial class Site2 : System.Web.UI.MasterPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      User u = Session["User"] as User;

      hlLvreoMasterList.Visible = u.UserRole == UserRole.SuperAdmin;
      hlLvreoMasterListEx.Visible = u.UserRole == UserRole.SuperAdmin;
      hlSdreoMasterList.Visible = u.UserRole == UserRole.SuperAdmin;
      hlMasterList.Visible = u.UserRole == UserRole.SuperAdmin;
      hlScrapping.Visible = u.UserRole == UserRole.SuperAdmin;
      hlWorkingList.Visible = u.UserRole == UserRole.SuperAdmin;
      hlEscrow.Visible = u.UserRole == UserRole.SuperAdmin;
      hlAdmin.Visible = u.UserRole == UserRole.SuperAdmin;
      hlGlobalGrid.Visible = u.UserRole == UserRole.SuperAdmin;
      hlRehab.Visible = u.UserRole == UserRole.SuperAdmin;
      hlProjectManagerGrid.Visible = u.UserRole == UserRole.ProjectManager;
      hlRentProjection.Visible = ((u.UserRole == UserRole.SuperAdmin) || (u.UserRole == UserRole.InvestorAdmin) || (u.UserRole == UserRole.InvestorUser));
      hlDashboard.Visible = ((u.UserRole == UserRole.SuperAdmin) || (u.UserRole == UserRole.InvestorUser) || (u.UserRole == UserRole.InvestorAdmin));
      hlMyInvestments.Visible = false;
      hlAvailableInvestments.Visible = false;

      if(u.UserRole == UserRole.User)
      {
        hlWorkingList.Visible = hlLvreoMasterList.Visible = u.LvreoRole != RoleAccess.NoAccess;
        hlSdreoMasterList.Visible = u.SdreoRole != RoleAccess.NoAccess;
        hlMasterList.Visible = u.MasterlistRole != RoleAccess.NoAccess;        
      }
      if (u.UserRole == UserRole.EscrowUser)
      {
          hlLvreoMasterList.Visible = true;
          hlSdreoMasterList.Visible = true;
          hlMasterList.Visible = true;
          hlWorkingList.Visible = true;
          hlLvreoMasterListEx.Visible = true;
          hlEscrow.Visible = true;

          // escrow юзер имеет доступ ко всем трем гридам (lvreo, sdreo и master)
          hlGlobalGrid.Visible = true;
          hlRehab.Visible = true;
      }
      else if (u.UserRole == UserRole.InvestorAdmin)
      {
        hlScrapping.Visible = true;        

        if (u.SubRole == UserSubRole.LVReo)
        {
          hlLvreoMasterList.Visible = true;
        }
        else
        {
          //hlSdreoMasterList.Visible = true;
        }

        hlMasterList.Visible = true;
        hlWorkingList.Visible = true;
      }
      else if (u.UserRole == UserRole.InvestorUser)
      {
        hlMyInvestments.Visible = true;
        hlAvailableInvestments.Visible = true;

        hlMasterList.Visible = true;
        hlWorkingList.Visible = true;
      }

      //Hide tab SDREO, 16.10.2013
      hlSdreoMasterList.Visible = false;

      lnkAdminMain.Visible = hlAdmin.Visible || hlScrapping.Visible;

      RefreshSubMenu();
    }

    private void RefreshSubMenu()
    {
      if (IsAdminPage())
      {
        divAdminSubMenu.Visible = true;
        tdAdminSubMenu.Visible = true;
        hlAdmin.CssClass = "top_menu_selected";

        string namePage = Page.GetType().FullName;
        hlRehabStatusCodes.CssClass = "";
        hlUserManagement.CssClass = "";
        hlDocumentManagement.CssClass = "";

        if (namePage == "ASP.admin_aspx")
        {
          hlRehabStatusCodes.CssClass = "sub_menu_selected";
        }
        else if (namePage == "ASP.usermanagement_aspx")
        {
          hlUserManagement.CssClass = "sub_menu_selected";
        }
        else if (namePage == "ASP.documentfolderlist_aspx")
        {
          hlDocumentManagement.CssClass = "sub_menu_selected";
        }
      }
      else
      {
        divAdminSubMenu.Visible = false;
        tdAdminSubMenu.Visible = false;
        hlAdmin.CssClass = "";
      }

      if (IsDashboardPage())
      {
          divDashboardSubMenu.Visible = true;
          tdDashboardSubMenu.Visible = true;
          hlDashboard.CssClass = "top_menu_selected";

          string namePage = Page.GetType().FullName;
          hlDashboard1.CssClass = "";
          hlDashboard3.CssClass = "";

          if (namePage == "ASP.dashboard_aspx")
          {
              hlDashboard1.CssClass = "sub_menu_selected";
          }
          else if (namePage == "ASP.dashboard2_aspx")
          {
              hlDashboard2.CssClass = "sub_menu_selected";
          }
          else if (namePage == "ASP.dashboard3_aspx")
          {
              hlDashboard3.CssClass = "sub_menu_selected";
          }
      }
      else
      {
          divDashboardSubMenu.Visible = false;
          tdDashboardSubMenu.Visible = false;
          hlDashboard.CssClass = "";
      }


    }

    private bool IsAdminPage()
    {
      string namePage = Page.GetType().FullName;
      bool result = (namePage == "ASP.admin_aspx") || (namePage == "ASP.usermanagement_aspx") || (namePage == "ASP.documentfolderlist_aspx");
      return result;
    }

    private bool IsDashboardPage()
    {
        string namePage = Page.GetType().FullName;
        bool result = (namePage == "ASP.dashboard_aspx") || (namePage == "ASP.dashboard2_aspx") || (namePage == "ASP.dashboard3_aspx");
        return result;
    }
  }
}